home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 21 / Cream of the Crop 21 (Terry Blount) (October 1996).iso / sound / rsynth22.zip / HPLAY.OLD < prev    next >
Text File  |  1996-09-13  |  5KB  |  195 lines

  1. #include <config.h>
  2. #include <stdio.h>
  3. #include <float.h>
  4. #include <os2.h>
  5. #define  INCL_OS2MM
  6. #include <os2me.h>
  7. #include "proto.h"
  8. #include "getargs.h"
  9. #include "hplay.h"
  10.  
  11. /* PlayList Entry */
  12.  
  13. typedef struct _PLE {
  14.     ULONG operation;
  15.     ULONG operand1;
  16.     ULONG operand2;
  17.     ULONG operand3;
  18. }PLE;
  19.  
  20. long samp_rate  = 8000;
  21. long bits = 8;
  22. int quiet = FALSE;
  23. MCI_OPEN_PARMS mop;
  24. PLE playlist[2];
  25.  
  26. void mci_err(ULONG rc)
  27. {
  28.     const rsize = 128;
  29.     char rbuff[rsize];
  30.  
  31.     ULONG rc2 = mciGetErrorString(rc,      // error code
  32.                                   rbuff,   // return buffer
  33.                                   rsize);  // rbuff size
  34.  
  35.     if (rc2 == MCIERR_SUCCESS)
  36.         fprintf(stderr,"MCI error: %s\n\n",rbuff);
  37.     else
  38.         fprintf(stderr,"error # %d has occured!\n\n", rc);
  39. }
  40.  
  41. int
  42. audio_init(int argc, char **argv)
  43. {
  44.  int rate;
  45.  
  46.  rate = 8;
  47.  argc = getargs("Audio Initialization", argc, argv,
  48.                 "r", "%d",  &rate,   "Sample Rate in kHz - 8, 11, 22, or 44",
  49.                 "Q", NULL, &quiet,   "+Q for no sound output (-Q Default)",
  50.                 "b", "%d", &bits, "Chunk size of playback sample 8 (def) or 16", 
  51.                 NULL);
  52.  if (help_only)
  53.   return (argc);
  54.  
  55.  switch(rate)
  56.     {
  57.      case  8 : samp_rate =  8000;
  58.                break;
  59.      case 11 : samp_rate = 11025;
  60.                break;
  61.      case 22 : samp_rate = 22050;
  62.                break;
  63.      case 44 : samp_rate = 44100;
  64.                break;
  65.      default : samp_rate =  8000;
  66.     }
  67.  
  68.   if (bits != 16)
  69.      bits = 8;
  70.  
  71.   return (argc);
  72. }
  73.  
  74. void
  75. audio_term(void)
  76. {
  77. }
  78.  
  79. void
  80. audio_play(int n, short *data)
  81. {
  82.  MCI_PLAY_PARMS mpp;
  83.  MCI_GENERIC_PARMS mgp;
  84.  MCI_WAVE_SET_PARMS wsp;
  85.  ULONG rc;
  86.  
  87.  if (!quiet)
  88.   {
  89.    unsigned char *plabuf;
  90.  
  91.    if (bits == 8)
  92.      {
  93.        plabuf = (unsigned char *) malloc(n);
  94.        if (plabuf)
  95.          {
  96.           unsigned char *p = plabuf;
  97.           unsigned char *e = p + n;
  98.           short temp;
  99.           while (p < e)
  100.             {
  101.              temp = *data / 128;
  102.              *p = temp + 128;
  103.              p++;
  104.              data++;
  105.             }
  106.          }
  107.        else 
  108.          {
  109.            fprintf(stderr, "Insufficient memory for Play Buffer\n\n");
  110.            return;
  111.          }
  112.      }
  113.    else
  114.       plabuf = (char *)data;
  115.    
  116.    playlist[0].operation = DATA_OPERATION;
  117.    playlist[0].operand1  = (long) plabuf;
  118.    playlist[0].operand2  = n;
  119.  
  120.    if(bits == 16) playlist[0].operand2 = n*2;
  121.  
  122.    playlist[0].operand3  = 0;
  123.    playlist[1].operation = EXIT_OPERATION;
  124.    mop.hwndCallback   = 0;
  125.    mop.usDeviceID     = 0;
  126.    mop.pszDeviceType  = MCI_DEVTYPE_WAVEFORM_AUDIO_NAME;
  127.    mop.pszElementName = (void *)&playlist[0];
  128.  
  129.    rc = mciSendCommand(0,
  130.                        MCI_OPEN,                        // open message
  131.                        MCI_WAIT | MCI_OPEN_SHAREABLE |  // message flags
  132.                        MCI_OPEN_PLAYLIST,
  133.                        &mop,                            // parameters
  134.                        0);
  135.  
  136.    if (rc != MCIERR_SUCCESS) mci_err(rc);
  137.  
  138.    // set device parameters
  139.    wsp.hwndCallback    = 0;
  140.    wsp.ulSamplesPerSec = samp_rate;
  141.    wsp.usBitsPerSample = bits;
  142.    wsp.usChannels = 1;
  143.  
  144.    rc = mciSendCommand(mop.usDeviceID,
  145.                        MCI_SET,
  146.                        MCI_WAIT |
  147.                        MCI_WAVE_SET_SAMPLESPERSEC, 
  148.                        &wsp,
  149.                        0);
  150.    if (rc != MCIERR_SUCCESS) mci_err(rc);
  151.    mpp.hwndCallback = 0;
  152.  
  153.    rc = mciSendCommand(mop.usDeviceID,
  154.                        MCI_SET,
  155.                        MCI_WAIT |
  156.                        MCI_WAVE_SET_BITSPERSAMPLE, 
  157.                        &wsp,
  158.                        0);
  159.    if (rc != MCIERR_SUCCESS) mci_err(rc);
  160.    mpp.hwndCallback = 0;
  161.  
  162.    rc = mciSendCommand(mop.usDeviceID,
  163.                        MCI_SET,
  164.                        MCI_WAIT |
  165.                        MCI_WAVE_SET_CHANNELS, 
  166.                        &wsp,
  167.                        0);
  168.  
  169.     if (rc != MCIERR_SUCCESS) mci_err(rc);
  170.     mpp.hwndCallback = 0;
  171.  
  172.     rc = mciSendCommand(mop.usDeviceID,
  173.                         MCI_PLAY,
  174.                         MCI_WAIT,
  175.                         &mpp,
  176.                         0);
  177.      if (rc != MCIERR_SUCCESS) mci_err(rc);
  178.      
  179.      // close device
  180.  
  181.      mgp.hwndCallback = 0;
  182.      rc = mciSendCommand(mop.usDeviceID,
  183.                         MCI_CLOSE,
  184.                         MCI_WAIT,
  185.                         &mgp,
  186.                         0);
  187.  
  188.      if (rc != MCIERR_SUCCESS) mci_err(rc);
  189.      if (bits == 8)
  190.         free(plabuf);
  191.      _control87(EM_INVALID | EM_DENORMAL | EM_ZERODIVIDE | EM_OVERFLOW | EM_UNDERFLOW | EM_INEXACT, MCW_EM);
  192.  
  193.    }
  194. }
  195.